package com.link.base.base.rsasecretkey;

import com.alibaba.fastjson.JSONObject;
import com.link.base.user.model.CoreUser;
import com.link.base.base.rsasecretkey.model.RSASecretKey;
import com.link.base.core.RSAUtils.DmpDecode;
import com.link.core.util.JsonUtil;
import com.link.core.util.UserUtil;
import com.link.base.core.basic.util.YmlConstants;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *  DMP密钥方法工具类
 * @author Ninglanhao
 * @date 2019/3/25 11:51
 */
public class SecretKeyHandler {
    /**
     *  获得当前用户登陆时的dmpEncode
     *
     * @author NingLanhao
     * @date 2019-03-25 14:10
     * @param headId 头ID
     * @param headIdName 头对象名称
     * @return java.util.Map
     */
    public static Map<String, Object> currentUserDmpEncode(String headIdName,String headId) throws Exception {
        CoreUser user = UserUtil.getUser();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId", user.getId());
        jsonObject.put("postnId", user.getPostnId());
        jsonObject.put("orgId", user.getOrgId());
        jsonObject.put("headIdName", headIdName);
        jsonObject.put("headId", headId);
        jsonObject.put("corpId", user.getLoginCorpId());
        Map<String, Object> result = getDmpEncode(jsonObject);
        return result;
    }

    /**
     * 根据Json对象生成dmpEncode
     *
     * @author NingLanhao
     * @date 2019-03-25 14:12
     * @param jsonObject json对象
     * @return java.util.Map
     */
    public static Map<String, Object> getDmpEncode(JSONObject jsonObject) throws Exception {
        Map<String, Object> result = new HashMap<>(16);
        RSASecretKey secretKey = new RSASecretKey();
        Date dNow = new Date();
        secretKey.setDate(dNow);
        secretKey.setEffectiveTime(YmlConstants.getDmpKeyExpires());
        secretKey.setMsg(jsonObject);
        String secretStr = JsonUtil.toString(secretKey);
        Map<String, String> map = DmpDecode.dmpEncode(secretStr);
        result.put("success", true);
        result.put("result", map);
        return result;
    }
}
